﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Maticsoft.DBUtility;//请先添加引用

namespace WebManager.Function
{
    public partial class MoveTo : System.Web.UI.Page
    {
        private string ColumnID = null;
        LYQ.BLL.SystemManagerTree treeBLL = new LYQ.BLL.SystemManagerTree();
        protected void Page_Load(object sender, EventArgs e)
        {
            ColumnID = HttpContext.Current.Request.QueryString["ColumnID"];
            if (!Page.IsPostBack)
            {
                BindData();
            }
        }

        protected void Zone_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindData();

        }

        protected void Save_Click(object sender, EventArgs e)
        {
            if (MoveSel.SelectedValue == "0")
            {
                string para = HttpContext.Current.Session["MoveTo"] as string;
                string[] sels = para.Split(',');

                string sql = null;
                if (Zone.SelectedValue == "eb8b49b9-e760-497b-83cf-d9fd1d9f0833")
                {
                    sql = "UPDATE [Doc] SET PID = '" + TreeView1.SelectedValue + "' WHERE oid='{0}'";
                }
                else
                {
                    sql = "UPDATE [Doc] SET PID = '" + TreeView1.SelectedValue + "',[Region]='" + UserAccount.SelectedValue + "' WHERE oid='{0}'";
                }

                foreach (string sel in sels)
                {
                    string sql1 = string.Format(sql, sel);
                    DbHelperSQL.ExecuteSql(sql1);
                }
            }
            else
            {
                //string sql = "";
                // if (Zone.SelectedValue == "eb8b49b9-e760-497b-83cf-d9fd1d9f0833")
                //{
                string sql = "UPDATE doc SET  pid='{0}', [Region]='{1}' WHERE [OID] in (SELECT oid FROM doc WHERE pid='{2}' )";
                sql = string.Format(sql, TreeView1.SelectedValue, UserAccount.SelectedValue, ColumnID);
                //sql = "UPDATE [Doc] SET PID = '" + TreeView1.SelectedValue + "' WHERE oid='{0}'";
                // }
                // else
                // {
                // sql = "UPDATE [Doc] SET PID = '" + TreeView1.SelectedValue + "',[Region]='" + UserAccount.SelectedValue + "' WHERE oid='{0}'";
                // }
                DbHelperSQL.ExecuteSql(sql);
            }

            string url = URLUtility.UrlBase + "/WebManager/Function/ListColumnContent.aspx?OID=" + ColumnID;
            Page.Response.Redirect(url);

        }

        protected void Cancle_Click(object sender, EventArgs e)
        {
            string url = URLUtility.UrlBase + "/WebManager/Function/ListColumnContent.aspx?OID=" + ColumnID;
            Page.Response.Redirect(url);
        }

        private void BindData()
        {
            if (Utility.CurrentSysUser().GID != "19800628-1111-0000-0000-000000000001" && Utility.CurrentSysUser().GID != "19800628-1111-0000-0000-000000000003")
            {
                Zone.Items.RemoveAt(0);
                Zone.Enabled = false;
            }

            UserAccount.Items.Clear();
            LYQ.BLL.S_User bll = new LYQ.BLL.S_User();
            UserAccount.DataTextField = "MDescription";
            UserAccount.DataValueField = "UName";
            if (Zone.SelectedValue == "eb8b49b9-e760-497b-83cf-d9fd1d9f0833")
            {
                // UserLabel.Visible = false;
                // UserAccount.Visible = false;
                UserAccount.DataSource = bll.GetList("uid !='19800628-1111-1111-1111-000000000001'").Tables[0];
                UserAccount.DataBind();
            }
            else if (Zone.SelectedValue == "fd870d78-0db5-48d5-af31-4d6acc3fa90d")
            {

                //UserLabel.Visible = true;
                //UserAccount.Visible = true;               
                UserAccount.DataSource = bll.GetList("(uid !='19800628-1111-1111-1111-000000000001') ").Tables[0];
                UserAccount.DataBind();
            }


            TreeNode root = new TreeNode();
            TreeView1.Nodes.Clear();
            if ((Utility.CurrentSysUser().GID != "19800628-1111-0000-0000-000000000001"
                && Utility.CurrentSysUser().GID != "19800628-1111-0000-0000-000000000003")
                || (Zone.SelectedValue == "fd870d78-0db5-48d5-af31-4d6acc3fa90d")
              )
            {
                root.Text = "地方版栏目";
                root.Value = "fd870d78-0db5-48d5-af31-4d6acc3fa90d";
                TreeView1.Nodes.Add(root);
                PopulateTreeView(root);
            }

            else
            {
                root.Text = "总站栏目";
                root.Value = "eb8b49b9-e760-497b-83cf-d9fd1d9f0833";
                TreeView1.Nodes.Add(root);
                PopulateTreeView(root);
            }
            TreeView1.ExpandDepth = 2;

        }

        private void PopulateTreeView(TreeNode parentNode)
        {
            string where = " ParentID = '" + parentNode.Value + "'";
            DataSet ds = treeBLL.GetList(where);
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                DataRow row = ds.Tables[0].Rows[i];
                TreeNode node = new TreeNode();
                node.Text = row["OName"] as string;
                node.Value = row["OID"] as string;
                parentNode.ChildNodes.Add(node);
                PopulateTreeView(node);
            }



        }

    }
}
